package com.cxq.finalproject.util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.cxq.finalproject.LoginActivity;

//用户表数据库类
public class MyUserHelper extends SQLiteOpenHelper{
    //数据库文件名称
    public static final String DB_NAME = "ClothLone";
    //表名
    public static final String TABLE_NAME = "user";
    //字段
    public static final String ID = "userId"; // 字段：ID
    public static final String PWD = "password"; // 字段：密码
    public static final String UserNAME = "userName"; //字段： 姓名
    public static final String GANDER = "sex"; //字段：性别
    // 调用父类构造器
    public MyUserHelper(Context context, String name, CursorFactory factory,
                        int version) {
        super(context, name, factory, version);
    }


    // 重写onCreate方法,首次执行时执行该方法，生成对应表
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 调用execSQL方法创建表
        db.execSQL("create table if not exists " + TABLE_NAME +
                " (" + ID + " varchar primary key," + PWD + " varchar,"
                + UserNAME + " varchar," + GANDER + " varchar)" );
        //如果id=1用户密码为1
        Cursor c = db.query(TABLE_NAME,null,"userId='1'",null,null,null,
                null);
        if (c.getCount() > 0){
            ContentValues values = new ContentValues();
            values.put(PWD,"1");
            db.update(TABLE_NAME,values,ID + "='1'",null);
        }else {
            insertUser(db,"1","1","简自豪","男性");
        }
        db.close();
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }


    public void insertUser(SQLiteDatabase db, String id, String pwd, String userNAME, String gander){
        ContentValues values = new ContentValues();
        values.put(ID,id);
        values.put(PWD,pwd);
        values.put(UserNAME,userNAME);
        values.put(GANDER,gander);
        long rid = db.insert(TABLE_NAME,ID,values);
        db.close();
    }
}
